<template>
  <el-container class="home-container">
    <el-header>
      <h1 class="logo">
        <img src="../assets/face.png" />
        <span>管理后台系统</span>
      </h1>
      <div class="btns">
        <el-button type="info" v-on:click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse? '64px': '200px' ">
        <div class="toggle-btn" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse-transition="false"
          :collapse="isCollapse"
          :router="true"
          :default-active="activeItem"
        >
          <!-- 一级菜单 -->
          <el-submenu v-bind:index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="setActiveState(subItem.path)"
            >
              <template>
                <i class="el-icon-menu"></i>
                <span slot="title">{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

          <!-- <el-menu-item index="3">
            <i class="el-icon-document"></i>
            <span slot="title">导航二</span>
          </el-menu-item>-->
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    // HelloWorld
  },
  data () {
    return {
      menuList: [],
      icons: {
        '101': 'el-icon-location',
        '201': 'el-icon-location',
        '301': 'el-icon-location',
        '401': 'el-icon-location',
        '501': 'el-icon-location'
      },
      isCollapse: false,
      activeItem: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenu () {
      const { data: res } = await this.$httpClient.get('home/menus')
      // console.log(res)
      this.menuList = res.data
    },
    toggleCollapse () {
      // console.log('')
      this.isCollapse = !this.isCollapse
      // console.log(this.isCollapse)
    },
    setActiveState (path) {
      // console.log(path)
      window.sessionStorage.setItem('activePath', path)
      this.activeItem = path
    }
  },
  created () {
    this.getMenu()
    this.activeItem = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100vh;
  min-width: 600px;
}
.el-header{
  background-color: #373d41;
  padding-left: 5px;
  display: flex;
  justify-content: space-between;
  h1{
    margin: 0.3rem 0;
    span{
      color: #fff;
      font-size: 1.5rem;
      vertical-align: 9px;
      padding-left: 14px;
    }
    img{height: 40px;}
  }
  .btns{ line-height: 60px;}
}
.el-aside {
  background-color: #333744;
  .el-menu{ border-right-style: none;}
  .toggle-btn { color: #fff; text-align: center; background-color: #4a5064; cursor: pointer; font-size: 14px; line-height: 26px; }
}
.el-main{ background-color: #eaedf1;}
</style>
